﻿using System.Windows;

namespace Loki.UI.Wpf
{
    public static class WpfExtensions
    {
        public static void Merge(this Style P_Style1, Style P_Style2)
        {
            if (!P_Style1.IsSealed && !P_Style2.IsSealed)
            {
                if (P_Style1.TargetType.IsAssignableFrom(P_Style2.TargetType))
                {
                    P_Style1.TargetType = P_Style2.TargetType;
                }

                if (P_Style2.BasedOn != null)
                {
                    Merge(P_Style1, P_Style2.BasedOn);
                }

                foreach (SetterBase L_CurrentSetter in P_Style2.Setters)
                {
                    P_Style1.Setters.Add(L_CurrentSetter);
                }

                foreach (TriggerBase L_CurrentTrigger in P_Style2.Triggers)
                {
                    P_Style1.Triggers.Add(L_CurrentTrigger);
                }

                // This code is only needed when using DynamicResources.
                foreach (object L_Key in P_Style2.Resources.Keys)
                {
                    P_Style1.Resources[L_Key] = P_Style2.Resources[L_Key];
                }
            }
        }
    }
}